﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace GDLanceCardEditor
{
    public partial class Form1 : Form
    {
        static log4net.ILog Logger = log4net.LogManager.GetLogger("MainLogger");
        private string cardRootPath = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog1.ShowDialog())
            {
                cardRootPath = folderBrowserDialog1.SelectedPath;
                DirectoryInfo cardRootDirectory = new DirectoryInfo(cardRootPath);
                DirectoryInfo[] cardInfoDirectoryList = cardRootDirectory.GetDirectories();
                foreach (DirectoryInfo cardInfoDirectory in cardInfoDirectoryList)
                {
                    StreamReader sr = new StreamReader(new FileStream(cardInfoDirectory.FullName + "/info_cn.json", FileMode.Open));
                    string line = sr.ReadLine();
                    if (line != "{")
                    {
                        Logger.Error("File[" + cardInfoDirectory.FullName + "/info_cn.json" + "] not start with '{'... Skip this directory.");
                        continue;
                    }

                    CardInfo cardInfo = new CardInfo();

                    while (line != "}")
                    {
                        line = sr.ReadLine().Trim();
                        string[] values = line.Split(':');
                        if (values[0] == "name")
                        {
                            cardInfo.name = values[1].Trim();
                        }
                        else if (values[0] == "number")
                        {
                            cardInfo.number = values[1].Trim();
                        }
                        else if (values[0] == "type")
                        {
                            cardInfo.type = values[1].Trim();
                        }
                        else if (values[0] == "star")
                        {
                            cardInfo.star = int.Parse(values[1].Trim());
                        }
                        else if (values[0] == "property")
                        {
                            cardInfo.property = values[1].Trim();
                        }
                        else if (values[0] == "race")
                        {
                            cardInfo.race = values[1].Trim();
                        }
                        else if (values[0] == "attack")
                        {
                            cardInfo.attack = int.Parse(values[1].Trim());
                        }
                        else if (values[0] == "defence")
                        {
                            cardInfo.defence = int.Parse(values[1].Trim());
                        }
                        else if (values[0] == "rare")
                        {
                            cardInfo.rare = values[1].Trim();
                        }
                        else if (values[0] == "package")
                        {
                            cardInfo.package = values[1].Trim();
                        }
                        else if (values[0] == "description")
                        {
                            cardInfo.description = values[1].Trim();
                        }
                    }
                }
            }
        }
    }

    [Serializable]
    [DataContract]
    public class CardInfo
    {
        public string name { get; set; }
        public string number { get; set; }
        public string type { get; set; }
        public int star { get; set; }
        public string property { get; set; }
        public string race { get; set; }
        public int attack { get; set; }
        public int defence { get; set; }
        public string rare { get; set; }
        public string[] package { get; set; }
        public string description { get; set; }
    }
}
